
package ui.activity.poscontrol.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import Bean.OrderDetailsNewBean;
import Utils.StringUtil;
import util.SysUtils;


/**
 * POS查询列表适配器
 * Created by chenjian on 19/2/27
 */
public class orderinfo2Adapter extends DelegateAdapter.Adapter<orderinfo2Adapter.ProfitVH> {

    private final Context mContext;
    private List<OrderDetailsNewBean.PosOrderShipVOListBean> datas = new ArrayList<>();

    PushBackPosAdapter adapter;
    public orderinfo2Adapter(Context context, List<OrderDetailsNewBean.PosOrderShipVOListBean> data) {
        mContext = context;
        datas = data;
    }

    public void setData(List<OrderDetailsNewBean.PosOrderShipVOListBean> data) {
        datas = data;
        notifyDataSetChanged();
    }


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new SingleLayoutHelper();
    }

    @NonNull
    @Override
    public ProfitVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ProfitVH(LayoutInflater.from(mContext).inflate(R.layout.item_orderinfo_list, viewGroup, false));
    }


    @Override
    public void onBindViewHolder(@NonNull ProfitVH holder, int position) {
        if (position < 0 || position >= datas.size()) {
            return;
        }
        OrderDetailsNewBean.PosOrderShipVOListBean item = datas.get(position);
        holder.trunk_time.setText(SysUtils.sf.format(new Date(item.getConsignTime())));//发货时间
        holder.trunk_num.setText( item.getNumber() + "台");//台数
        holder.trunk_money.setText(StringUtil.getFormatPrice(item.getBackAmount() )+ "元");//返现金额
        holder.trunk_day.setText( item.getExpireTime() + "天");//有效期

        LinearLayoutManager manger = new LinearLayoutManager(mContext);
        manger.setOrientation(OrientationHelper.VERTICAL);
        holder.order_sn.setLayoutManager(manger);
        List<String> newSnList = new ArrayList<>();
        newSnList.addAll(item.getSnList());
        newSnList.remove(0);
        OrderPosInfoAdapter adapter = new OrderPosInfoAdapter(mContext, newSnList);
        holder.order_sn.setAdapter(adapter);
        if(item.getSnList().size()>0) {
            holder.pos_txt.setText(item.getSnList().get(0));
        }
        holder.group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.pos_txt.setSelected(!holder.pos_txt.isSelected());
                if ( holder.pos_txt.isSelected()) {
                    Drawable drawable = mContext.getResources().getDrawable(R.mipmap.learn_top);
                    holder.xialabtn.setBackground(drawable);
                    holder.order_sn_relative.setVisibility(View.VISIBLE);
                } else {
                    Drawable drawable = mContext.getResources().getDrawable(R.mipmap.learn_bottom);
                    holder.xialabtn.setBackground(drawable);
                    holder.order_sn_relative.setVisibility(View.GONE);
                }
            }
        });



    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ProfitVH extends RecyclerView.ViewHolder {
        TextView trunk_time;
        TextView trunk_num;
        TextView trunk_day;
        TextView trunk_money;
        TextView pos_txt;
        RecyclerView order_sn;
        RelativeLayout order_sn_relative;
        ImageView xialabtn;
        LinearLayout group;

        ProfitVH(@NonNull View itemView) {
            super(itemView);
            group = itemView.findViewById(R.id.group);
            order_sn = itemView.findViewById(R.id.order_sn);
            trunk_time = itemView.findViewById(R.id.trunk_time);
            trunk_num = itemView.findViewById(R.id.trunk_num);
            trunk_day = itemView.findViewById(R.id.trunk_day);
            trunk_money = itemView.findViewById(R.id.trunk_money);
            pos_txt = itemView.findViewById(R.id.pos_txt);
            xialabtn = itemView.findViewById(R.id.xialabtn);
            order_sn_relative=itemView.findViewById(R.id.order_sn_relative);


        }
    }
}
